package com.github.cakin.shiro.chapter23.dao;

import com.github.cakin.shiro.chapter23.entity.App;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @className: AppDaoImpl
 * @description: App数据访问层实现类
 * @date: 2020/6/1
 * @author: cakin
 */
@Repository
public class AppDaoImpl implements AppDao {
    /**
     * jdbcTemplate
     */
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 功能描述：创建App
     *
     * @param app app应用
     * @return App 创建的App
     * @author cakin
     * @date 2020/6/1
     * @description:
     */
    public App createApp( final App app ) {
        final String sql = "insert into sys_app(name, app_key, app_secret, available) values(?,?,?,?)";

        GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement( Connection connection ) throws SQLException {
                PreparedStatement psst = connection.prepareStatement(sql, new String[]{"id"});
                int count = 1;
                psst.setString(count++, app.getName());
                psst.setString(count++, app.getAppKey());
                psst.setString(count++, app.getAppSecret());
                psst.setBoolean(count++, app.getAvailable());
                return psst;
            }
        }, keyHolder);
        app.setId(keyHolder.getKey().longValue());
        return app;
    }

    /**
     * 功能描述：更新App
     *
     * @param app app应用
     * @return App 修改后的App
     * @author cakin
     * @date 2020/6/1
     */
    public App updateApp( App app ) {
        final String sql = "update sys_app set name=?, app_key=?, app_secret=?, available=? where id=?";
        jdbcTemplate.update(
                sql,
                app.getName(), app.getAppKey(), app.getAppSecret(), app.getAvailable(), app.getId());
        return app;
    }

    /**
     * 功能描述：删除App
     *
     * @param appId app主键
     * @author cakin
     * @date 2020/6/1
     */
    public void deleteApp( Long appId ) {
        final String sql = "delete from sys_app where id=?";
        jdbcTemplate.update(sql, appId);
    }


    /**
     * 功能描述：根据appId找到对应的App
     *
     * @param appId appId
     * @return App App
     * @author cakin
     * @date 2020/6/1
     */
    public App findOne( Long appId ) {
        final String sql = "select id, name, app_key, app_secret, available from sys_app where id=?";
        List<App> appList = jdbcTemplate.query(sql, new BeanPropertyRowMapper(App.class), appId);
        if (appList.size() == 0) {
            return null;
        }
        return appList.get(0);
    }

    /**
     * 功能描述：找到所有的App
     *
     * @return List<App> App列表
     * @author cakin
     * @date 2020/6/1
     */
    public List<App> findAll() {
        final String sql = "select id, name, app_key, app_secret, available from sys_app";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper(App.class));
    }

    /**
     * 功能描述：根据appKey找到对应的主键
     *
     * @param appKey app键
     * @return List<App> App列表
     * @author cakin
     * @date 2020/6/1
     */
    public Long findAppIdByAppKey( String appKey ) {
        final String sql = "select id from sys_app where app_key=?";
        List<Long> appIdList = jdbcTemplate.queryForList(sql, Long.class, appKey);
        if (appIdList.size() == 0) {
            return null;
        }
        return appIdList.get(0);
    }
}
